package com.ittx.dzt.shein.consumer.handler;

import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.EdiBatchSplitDto;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.handler.IPlatformApiHandler;
import com.ittx.dzt.edi.handler.PlatformApiHandlerFactory;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.SpringUtil;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Component;

/**
 * 希音
 * <p>
 *
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/8/18
 */
@Slf4j
@Component
public class SheinSemiBatchSplitHandler {

	public void onMessage(String message, String logStr) {
		try {
			log.debug("希音[{}]拆分处理消费者入参:[{}]", logStr, message);
			EdiBatchSplitDto ediBatchSplitDto = JacksonTool.toEntity(message, EdiBatchSplitDto.class);
			PlatformApiParamDTO platformApiParam = ediBatchSplitDto.getPlatformApiParamDTO();
			platformApiParam.setPlatformIdentifierTag(ediBatchSplitDto.getPlatformIdentifierTag());
			//拆分后使用单号模式下载订单
			platformApiParam.setQueryType(PlatformApiConstant.ApiByTime.ID);
			String platformCode = platformApiParam.getStoreConfig().getPlatformCode();
			Class clazz = PlatformApiHandlerFactory.getInvokeHandler(platformCode, platformApiParam.getApi());
			if (clazz == null) {
				String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0001_MSG, platformApiParam.getRequestId(), platformCode, platformApiParam.getApi());
				log.error(msg);
			}
			IPlatformApiHandler platformApiHandler = (IPlatformApiHandler) SpringUtil.getBean(clazz);
			Result<?> result = platformApiHandler.process(platformApiParam);
			if (result.hasError()) {
				log.error("希音[{}]拆分后:[{}]拉取数据失败:[{}]", logStr, message, JacksonTool.toJson(result));
			}
		} catch (Exception e) {
			log.error("希音[{}]拆分处理消费者入参:[{}]", logStr, message);
			log.error("希音拆分数据请求异常", e);
		}

	}
}
